Ejemplo n.º 1
0
        public async Task <object> Register(UserSignUpDTO usr)
        {
            #region CheckEmailExist
            if (await _context.Users.AnyAsync(u => u.Email == usr.Email))
            {
                ModelState.AddModelError("Exist email", "This email had been registered");
            }
            #endregion

            #region CheckPasswordIsInvalid
            if (!PasswordValid.Valid(usr.Password))
            {
                ModelState.AddModelError("Password", PasswordValid.Message);
            }
            #endregion

            #region CheckModelIsInvalid
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            #endregion

            usr.Password = CryptoHelper.Crypto.HashPassword(usr.Password);
            User user = new User();
            try
            {
                user           = _mapper.Map <User>(usr);
                user.CreatedAt = DateTime.UtcNow.AddHours(4);
                await _context.Users.AddAsync(user);

                await _context.SaveChangesAsync();
            }
            catch (Exception)
            {
                return(StatusCode(409));
            }
            return(Ok(_mapper.Map <UserGetDTO>(user)));
        }
Ejemplo n.º 2
0
        public async Task <object> Create(PlaceCreateModel plc)
        {
            #region CheckModelIsInvalid
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            #endregion

            #region CheckUserIsNull
            if (!await _context.Users.AnyAsync(u => u.Id == User.GetUserId()))
            {
                return(NotFound());
            }
            #endregion

            plc.Place.UserId   = User.GetUserId();
            plc.Place.CreateAt = DateTime.UtcNow.AddHours(4);
            Place place = new Place();

            /*Try to photo upload,add to context and savecAhnges*/
            try
            {
                if (!string.IsNullOrWhiteSpace(plc.Place.Photo) && !string.IsNullOrWhiteSpace(plc.Place.PhotoFileName))
                {
                    plc.Place.Photo = FileManager.Upload(plc.Place.Photo, plc.Place.PhotoFileName);
                }

                place = _mapper.Map <Place>(plc.Place);
                await _context.Places.AddAsync(place);

                await _context.SaveChangesAsync();
            }
            catch (Exception)
            {
                return(StatusCode(409));
            }

            /*Create PlaceTags*/
            if (plc.Tags != null && plc.Tags.Count > 0)
            {
                foreach (var tag in plc.Tags)
                {
                    #region CehckTagsNull
                    if (!await _context.Tags.AnyAsync(t => t.Id == tag.Id))
                    {
                        continue;
                    }
                    #endregion

                    PlacesTags placeTag = new PlacesTags
                    {
                        TagId   = tag.Id,
                        PlaceId = place.Id
                    };

                    //PlaceTags add to context
                    try
                    {
                        await _context.PlacesTags.AddAsync(placeTag);

                        await _context.SaveChangesAsync();
                    }
                    catch (Exception)
                    {
                        continue;
                    }
                }
            }

            /*Create PlaceTags*/
            if (plc.Socials != null && plc.Socials.Count > 0)
            {
                foreach (var social in plc.Socials)
                {
                    #region CehckSocialIsEmpty
                    if (string.IsNullOrWhiteSpace(social.Link) || string.IsNullOrWhiteSpace(social.Name))
                    {
                        continue;
                    }
                    #endregion

                    Social soc = new Social
                    {
                        Link    = social.Link,
                        Name    = social.Name,
                        PlaceId = place.Id,
                        Type    = SocialType.Place
                    };

                    //Social add to context
                    try
                    {
                        await _context.Socials.AddAsync(soc);

                        await _context.SaveChangesAsync();
                    }
                    catch (Exception)
                    {
                        continue;
                    }
                }
            }

            /*Create Slider Photos*/
            if (plc.SliderPhotos != null && plc.SliderPhotos.Count > 0)
            {
                foreach (var slider in plc.SliderPhotos)
                {
                    #region CehckSliderIsEmpty
                    if (string.IsNullOrWhiteSpace(slider.Photo) || string.IsNullOrWhiteSpace(slider.PhotoName))
                    {
                        continue;
                    }
                    #endregion

                    PlaceSliderPhotos photo = new PlaceSliderPhotos();
                    photo.PlaceId = place.Id;
                    try
                    {
                        photo.Photo     = FileManager.Upload(slider.Photo, slider.PhotoName);
                        photo.PhotoName = slider.PhotoName;

                        await _context.PlaceSliderPhotos.AddAsync(photo);

                        await _context.SaveChangesAsync();
                    }
                    catch (Exception)
                    {
                        continue;
                    }
                }
            }

            /*Create PlaceFaqs*/
            if (plc.Faqs != null && plc.Faqs.Count > 0)
            {
                foreach (var faq in plc.Faqs)
                {
                    #region CehckFaqIsEmpty
                    if (string.IsNullOrWhiteSpace(faq.Answer) || string.IsNullOrWhiteSpace(faq.Question))
                    {
                        continue;
                    }
                    #endregion

                    PlaceFaq newFaq = new PlaceFaq
                    {
                        Answer   = faq.Answer,
                        Question = faq.Question,
                        PlaceId  = place.Id
                    };

                    //Faq add to context
                    try
                    {
                        await _context.PlaceFaqs.AddAsync(newFaq);

                        await _context.SaveChangesAsync();
                    }
                    catch (Exception)
                    {
                        continue;
                    }
                }
            }

            /*Create WorkHours*/
            if (plc.WorkHours != null && plc.WorkHours.Count > 0)
            {
                foreach (var hour in plc.WorkHours)
                {
                    WorkHour workHour = new WorkHour
                    {
                        Day     = hour.Day,
                        Open    = hour.Open,
                        Close   = hour.Close,
                        PlaceId = place.Id
                    };

                    //WorkHour add to context
                    try
                    {
                        await _context.WorkHours.AddAsync(workHour);

                        await _context.SaveChangesAsync();
                    }
                    catch (Exception)
                    {
                        continue;
                    }
                }
            }

            return(StatusCode(201));
        }
Ejemplo n.º 3
0
        public async Task <object> Put(UserUpdateDTO usr)
        {
            #region CheckIdsIsNotSame
            if (User.GetUserId() != usr.Id)
            {
                return(StatusCode(401, "User cannot update another user information"));
            }
            #endregion

            User user = _context.Users.Find(User.GetUserId());

            #region CheckIsNull
            if (user == null)
            {
                return(NotFound());
            }
            #endregion

            #region CheckEmailIsExisting
            if (_context.Users.Any(u => u.Email == usr.Email && u.Id != usr.Id))
            {
                ModelState.AddModelError("EmailExisting", "This email had been registered");
            }
            #endregion

            #region CheckPasswordsAreNotSame
            if (usr.Password.Trim() != usr.NewPassword.Trim())
            {
                ModelState.AddModelError("Password", "New Passwords are different");
            }
            #endregion

            #region CheckModelIsInvalid
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            #endregion

            user.Address  = usr.Address;
            user.Email    = usr.Email;
            user.FullName = usr.FullName;
            user.Gender   = usr.Gender;
            user.Phone    = usr.Phone;
            user.Website  = usr.Phone;

            if (!string.IsNullOrWhiteSpace(usr.Password) && !string.IsNullOrWhiteSpace(usr.NewPassword))
            {
                user.Password = CryptoHelper.Crypto.HashPassword(usr.Password);
            }

            /*Crete or Update User's socials*/
            if (usr.Socials != null)
            {
                foreach (var scl in usr.Socials)
                {
                    if (string.IsNullOrWhiteSpace(scl.Id))
                    {
                        if (!await _context.Socials.AnyAsync(s => s.UserId == user.Id && s.Name == scl.Name && s.Link == scl.Link))
                        {
                            Social social = new Social
                            {
                                Name   = scl.Name,
                                Link   = scl.Link,
                                Type   = SocialType.User,
                                UserId = usr.Id
                            };
                            await _context.Socials.AddAsync(social);
                        }
                        continue;
                    }
                    else
                    {
                        Social social = _context.Socials.Find(scl.Id);
                        if (social != null && social.UserId == usr.Id && social.Type == SocialType.User)
                        {
                            social.Name = scl.Name;
                            social.Link = scl.Link;
                        }
                    }
                    try
                    {
                        await _context.SaveChangesAsync();
                    }
                    catch (Exception)
                    {
                        return(StatusCode(409));
                    }
                }
            }


            try
            {
                await _context.SaveChangesAsync();
            }
            catch (Exception)
            {
                return(StatusCode(409));
            }
            return(NoContent());
        }