package dao import ( "bufio" "encoding/json" "errors" "fmt" "io" "io/ioutil" "os" "github.com/google/uuid" "github.com/willie68/schematic-service-go/model" ) // SimpleDAO a file based dao type SimpleDAO struct { Path string initialised bool tags []string manufacturers []string users map[string]string } // AddFile adding a file to the filestorage func (s *SimpleDAO) AddFile(filename string, reader io.Reader) (string, error) { id := uuid.New().String() filesPath := s.Path + "/files/" + id err := os.MkdirAll(filesPath, os.ModePerm) if err != nil { return "", err } f, err := os.Create(fmt.Sprintf("%s/%s", filesPath, filename)) defer f.Close() writer := bufio.NewWriter(f) _, err = writer.ReadFrom(reader) if err != nil { return "", err } err = writer.Flush() if err != nil { return "", err } fmt.Printf("path %s\n", filesPath) return id, nil } //CreateSchematic creates a new schematic file func (s *SimpleDAO) CreateSchematic(schematic model.Schematic) (string, error) { id := uuid.New().String() filesPath := s.Path + "/schematics/" + id err := os.MkdirAll(filesPath, os.ModePerm) if err != nil { return "", err } schematicJson, _ := json.Marshal(schematic) filesPath = filesPath + "/schematic.json" err = ioutil.WriteFile(filesPath, schematicJson, 0644) if err != nil { return "", err } return id, nil } //GetSchematic getting a schematic from an id func (s *SimpleDAO) GetSchematic(schematicID string) (model.Schematic, error) { return model.Schematic{}, errors.New("not implemented yet") } //GetFile getting a stream of a file func (s *SimpleDAO) GetFile(fileid string, stream io.Writer) error { return errors.New("not implemented yet") } //GetSchematics query the schematics func (s *SimpleDAO) GetSchematics(query string, offset int, limit int, owner string) ([]model.Schematic, error) { return nil, errors.New("not implemented yet") } //CreateTag create a new tag func (s *SimpleDAO) CreateTag(tag string) error { return errors.New("not implemented yet") } //GetTags getting all tags func (s *SimpleDAO) GetTags() []string { return nil } //GetTagsCount getting the count of all tags func (s *SimpleDAO) GetTagsCount() int { return 0 } //CreateManufacturer create a new manufacturer func (s *SimpleDAO) CreateManufacturer(manufacturer string) error { return errors.New("not implemented yet") } //GetManufacturers getting all manufacturers func (s *SimpleDAO) GetManufacturers() []string { return nil } //GetManufacturersCount getting the count of all manufacturers func (s *SimpleDAO) GetManufacturersCount() int { return 0 } //CheckUser check user password func (s *SimpleDAO) CheckUser(username string, password string) bool { return true } //GetUser getting an user model func (s *SimpleDAO) GetUser(username string) (model.User, bool) { return model.User{}, false } //AddUser adding a new user func (s *SimpleDAO) AddUser(user model.User) error { return errors.New("not implemented yet") } //DeleteUser delete an user func (s *SimpleDAO) DeleteUser(username string) error { return errors.New("not implemented yet") } //ChangePWD change the password of a user func (s *SimpleDAO) ChangePWD(username string, newpassword string, oldpassword string) error { return errors.New("not implemented yet") } //DropAll drop all data func (s *SimpleDAO) DropAll() { } //Ping short ping if file system is availble func (s *SimpleDAO) Ping() error { return errors.New("not implemented yet") } //Stop this service func (s *SimpleDAO) Stop() { }