Exemple #1
0
        public int CreateSublet(FullSubletModel fsm)
        {
            try
            {
                SubletDataEntity sde = _mapper.ExtractDataEntity(fsm);
                SubletModel      sm  = new SubletModel(fsm.Email, fsm.Address, fsm.Description, fsm.ImageUrl, fsm.Price, fsm.Rating);

                var newSub = _context.Sublets.Add(sm);
                _context.SaveChanges();

                sde.SubletID = newSub.Entity.ID;
                fsm.ID       = newSub.Entity.ID;
                _context.SubletData.Add(sde);
                _context.SaveChanges();

                UpdateTags(fsm);
                CreateRoommates(fsm);

                SubletType esSublet = new SubletType
                {
                    ID          = fsm.ID,
                    Address     = fsm.Address,
                    Description = fsm.Description,
                    Tags        = fsm.Tags
                };
                _elastic.IndexSublet(esSublet);

                return(newSub.Entity.ID);
            }
            catch (DbUpdateException e)
            {
                throw new DbUpdateException("error", e);
            }
        }
Exemple #2
0
        public SubletDataEntity ExtractDataEntity(FullSubletModel model)
        {
            SubletDataEntity sde = new SubletDataEntity
            {
                Description = model.Description,
                IsFurnished = model.IsFurnished,
                Email       = model.Email,
                OpenHouse   = model.OpenHouse
            };

            return(sde);
        }
 public int Put(int id, [FromBody] FullSubletModel value)
 {
     if (!ModelState.IsValid)
     {
         Console.WriteLine("Model state is invalid");
         return(-1);
     }
     else
     {
         return(_subletRepo.UpdateSublet(id, value));
     }
 }
 public int?PostFull([FromBody] FullSubletModel value)
 {
     if (!ModelState.IsValid)
     {
         Console.WriteLine("Model state is invalid");
         return(-1);
     }
     else
     {
         return(_subletRepo.CreateSublet(value));
     }
 }
Exemple #5
0
 public void FillNulls(FullSubletModel oldFsm, FullSubletModel fsm)
 {
     foreach (var prop in fsm.GetType().GetProperties())
     {
         if (prop.CanRead)
         {
             if (prop.GetValue(fsm) == null)
             {
                 prop.SetValue(fsm, prop.GetValue(oldFsm));
             }
         }
     }
 }
Exemple #6
0
        public int UpdateSublet(int id, FullSubletModel fsm)
        {
            try
            {
                FullSubletModel oldFsm = GetFullSublet(id);
                fsm.Email = oldFsm.Email;
                fsm.ID    = oldFsm.ID;

                _mapper.FillNulls(oldFsm, fsm);

                SubletDataEntity sde   = _mapper.ExtractDataEntity(fsm);
                SubletModel      sm    = new SubletModel(fsm.Email, fsm.Address, fsm.Description, fsm.ImageUrl, fsm.Price, fsm.Rating);
                SubletModel      oldSm = _context.Sublets.Where(s => s.ID == id).FirstOrDefault();
                oldSm.Address     = sm.Address;
                oldSm.Description = sm.Description;
                oldSm.Price       = sm.Price;
                oldSm.Rating      = sm.Rating;
                oldSm.ImageUrl    = sm.ImageUrl;

                _context.Sublets.Update(oldSm);
                _context.SaveChanges();

                SubletDataEntity oldSde = _context.SubletData.Where(sd => sd.SubletID == id).FirstOrDefault();
                oldSde.SubletID    = id;
                oldSde.Email       = sde.Email;
                oldSde.IsFurnished = sde.IsFurnished;
                oldSde.Description = sde.Description;
                oldSde.OpenHouse   = sde.OpenHouse;

                _context.SubletData.Update(oldSde);
                _context.SaveChanges();


                UpdateTags(fsm);
                UpdateRoommates(fsm);
                SubletType esSublet = new SubletType
                {
                    ID          = fsm.ID,
                    Description = fsm.Description,
                    Address     = fsm.Address,
                    Tags        = fsm.Tags
                };

                _elastic.IndexSublet(esSublet);
                return(id);
            }
            catch (DbUpdateException e)
            {
                throw new DbUpdateException("error", e);
            }
        }
Exemple #7
0
        public FullSubletModel Map(SubletModel model, SubletDataEntity entity, string[] tags, RoommateEntity[] roommates)
        {
            FullSubletModel fsm = new FullSubletModel(model.ID, model.Email, model.Address, model.Price)
            {
                Description = entity.Description,
                IsFurnished = entity.IsFurnished,
                Roommates   = roommates,
                OpenHouse   = entity.OpenHouse,
                Tags        = tags,
                ImageUrl    = model.ImageUrl,
                Rating      = model.Rating
            };

            return(fsm);
        }
Exemple #8
0
        public FullSubletModel GetFullSublet(int id)
        {
            SubletModel      sm  = _context.Sublets.Where(s => s.ID == id).FirstOrDefault();
            SubletDataEntity sde = _context.SubletData.Where(sd => sd.SubletID == id && sd.Email.Equals(sm.Email)).FirstOrDefault();

            List <string> tags = (from t in _context.Tags
                                  join ti in _context.TagIndex on t.TagID equals ti.ID into t_ti
                                  from s in t_ti.DefaultIfEmpty()
                                  where t.SubletID == id
                                  select s.Tag).ToList();

            RoommateEntity[] roommates = _context.Roommates.Where(r => r.SubletID == id).ToArray();
            FullSubletModel  fsm       = _mapper.Map(sm, sde, tags.ToArray(), roommates);

            return(fsm);
        }
Exemple #9
0
 public void CreateRoommates(FullSubletModel fsm)
 {
     try
     {
         foreach (RoommateEntity r in fsm.Roommates)
         {
             r.SubletID = fsm.ID;
             _context.Roommates.Add(r);
             _context.SaveChanges();
         }
     }
     catch (DbUpdateException e)
     {
         throw new DbUpdateException("error", e);
     }
 }
Exemple #10
0
        public void UpdateRoommates(FullSubletModel fsm)
        {
            try
            {
                if (_context.Roommates.Any(i => i.SubletID == fsm.ID))
                {
                    var oldRoommates = _context.Roommates.Where(i => i.SubletID == fsm.ID).ToArray();
                    _context.Roommates.RemoveRange(oldRoommates);
                    _context.SaveChanges();
                }

                CreateRoommates(fsm);
            }
            catch (DbUpdateException e)
            {
                throw new DbUpdateException("error", e);
            }
        }
Exemple #11
0
        public void UpdateTags(FullSubletModel fsm)
        {
            try
            {
                if (_context.Tags.Any(t => t.SubletID == fsm.ID))
                {
                    var collection = _context.Tags.Where(t => t.SubletID == fsm.ID).ToArray();
                    // remove all tag associations
                    _context.Tags.RemoveRange(collection);
                    _context.SaveChanges();
                }

                foreach (string s in fsm.Tags)
                {
                    if (!_context.TagIndex.Any(ti => ti.Tag == s))
                    {
                        // tag not found, add tag to index
                        TagIndexEntity tagIndexToAdd = new TagIndexEntity {
                            Tag = s
                        };
                        _context.TagIndex.Add(tagIndexToAdd);
                        _context.SaveChanges();
                    }
                    // add association
                    int       tagToAddID = _context.TagIndex.Where(t => t.Tag == s).First().ID;
                    TagEntity tagToAdd   = new TagEntity {
                        SubletID = fsm.ID, TagID = tagToAddID
                    };

                    _context.Tags.Add(tagToAdd);
                    _context.SaveChanges();
                }
            }
            catch (DbUpdateException e)
            {
                throw new DbUpdateException("error", e);
            }
        }