public IActionResult CreatePlace([FromBody] CreatePlaceModel place)
        {
            if (place == null || place.Title == null || place.Country == null || place.City == null ||
                place.Street == null || place.Number == null)
            {
                return(BadRequest("Invalid request"));
            }

            string get_email          = "";
            var    identity           = HttpContext.User.Identity as ClaimsIdentity;
            IEnumerable <Claim> claim = identity.Claims;
            var usernameClaim         = claim
                                        .Where(x => x.Type == ClaimTypes.Name)
                                        .FirstOrDefault();

            if (usernameClaim != null)
            {
                get_email = usernameClaim.Value;
            }
            if (get_email.Length < 1)
            {
                return(Unauthorized("Invalid user"));
            }

            Users user = LoadUser(get_email);

            if (user == null)
            {
                return(Unauthorized("Invalid user"));
            }

            if (place.Title.Length < 1)
            {
                return(Unauthorized("Missing title"));
            }
            if (place.Country.Length < 1)
            {
                return(Unauthorized("Missing country name"));
            }
            if (place.City.Length < 1)
            {
                return(Unauthorized("Missing city name"));
            }
            if (place.Street.Length < 1)
            {
                return(Unauthorized("Missing street name"));
            }
            if (place.Number.Length < 1)
            {
                return(Unauthorized("Missing number name"));
            }

            if (String.IsNullOrEmpty(place.Description))
            {
                place.Description = null;
            }

            TimeSpan timeOpen;
            TimeSpan timeClose;

            if (place.TimeOC1 != null)
            {
                timeOpen  = TimeSpan.Parse(place.TimeOC1[0]);
                timeClose = TimeSpan.Parse(place.TimeOC1[1]);
                if (timeOpen >= timeClose)
                {
                    return(Unauthorized("Wrong time"));
                }
            }

            if (place.TimeOC2 != null)
            {
                timeOpen  = TimeSpan.Parse(place.TimeOC2[0]);
                timeClose = TimeSpan.Parse(place.TimeOC2[1]);
                if (timeOpen >= timeClose)
                {
                    return(Unauthorized("Wrong time"));
                }
            }

            if (place.TimeOC3 != null)
            {
                timeOpen  = TimeSpan.Parse(place.TimeOC3[0]);
                timeClose = TimeSpan.Parse(place.TimeOC3[1]);
                if (timeOpen >= timeClose)
                {
                    return(Unauthorized("Wrong time"));
                }
            }

            if (place.TimeOC4 != null)
            {
                timeOpen  = TimeSpan.Parse(place.TimeOC4[0]);
                timeClose = TimeSpan.Parse(place.TimeOC4[1]);
                if (timeOpen >= timeClose)
                {
                    return(Unauthorized("Wrong time"));
                }
            }

            if (place.TimeOC5 != null)
            {
                timeOpen  = TimeSpan.Parse(place.TimeOC5[0]);
                timeClose = TimeSpan.Parse(place.TimeOC5[1]);
                if (timeOpen >= timeClose)
                {
                    return(Unauthorized("Wrong time"));
                }
            }

            if (place.TimeOC6 != null)
            {
                timeOpen  = TimeSpan.Parse(place.TimeOC6[0]);
                timeClose = TimeSpan.Parse(place.TimeOC6[1]);
                if (timeOpen >= timeClose)
                {
                    return(Unauthorized("Wrong time"));
                }
            }

            if (place.TimeOC7 != null)
            {
                timeOpen  = TimeSpan.Parse(place.TimeOC7[0]);
                timeClose = TimeSpan.Parse(place.TimeOC7[1]);
                if (timeOpen >= timeClose)
                {
                    return(Unauthorized("Wrong time"));
                }
            }

            using (var _context = new AppDBContext())
            {
                using (var _contextTransaction = _context.Database.BeginTransaction())
                {
                    try
                    {
                        Place new_place = new Place();
                        new_place.id_user     = user.id_user;
                        new_place.title       = place.Title;
                        new_place.description = place.Description;

                        new_place.multi_time           = true;
                        new_place.Place_data_multitime = new List <Place_data_multitime>();

                        if (place.TimeOC1 != null)
                        {
                            Place_data_multitime new_multitime = new Place_data_multitime();
                            new_multitime.day_week   = E_day_week.MONDAY;
                            new_multitime.start_date = TimeSpan.Parse($"{place.TimeOC1[0]}:00");
                            new_multitime.end_date   = TimeSpan.Parse($"{place.TimeOC1[1]}:00");
                            new_place.Place_data_multitime.Add(new_multitime);
                        }

                        if (place.TimeOC2 != null)
                        {
                            Place_data_multitime new_multitime = new Place_data_multitime();
                            new_multitime.day_week   = E_day_week.TUESDAY;
                            new_multitime.start_date = TimeSpan.Parse($"{place.TimeOC2[0]}:00");
                            new_multitime.end_date   = TimeSpan.Parse($"{place.TimeOC2[1]}:00");
                            new_place.Place_data_multitime.Add(new_multitime);
                        }

                        if (place.TimeOC3 != null)
                        {
                            Place_data_multitime new_multitime = new Place_data_multitime();
                            new_multitime.day_week   = E_day_week.WEDNESDAY;
                            new_multitime.start_date = TimeSpan.Parse($"{place.TimeOC3[0]}:00");
                            new_multitime.end_date   = TimeSpan.Parse($"{place.TimeOC3[1]}:00");
                            new_place.Place_data_multitime.Add(new_multitime);
                        }

                        if (place.TimeOC4 != null)
                        {
                            Place_data_multitime new_multitime = new Place_data_multitime();
                            new_multitime.day_week   = E_day_week.THURSDAY;
                            new_multitime.start_date = TimeSpan.Parse($"{place.TimeOC4[0]}:00");
                            new_multitime.end_date   = TimeSpan.Parse($"{place.TimeOC4[1]}:00");
                            new_place.Place_data_multitime.Add(new_multitime);
                        }

                        if (place.TimeOC5 != null)
                        {
                            Place_data_multitime new_multitime = new Place_data_multitime();
                            new_multitime.day_week   = E_day_week.FRIDAY;
                            new_multitime.start_date = TimeSpan.Parse($"{place.TimeOC5[0]}:00");
                            new_multitime.end_date   = TimeSpan.Parse($"{place.TimeOC5[1]}:00");
                            new_place.Place_data_multitime.Add(new_multitime);
                        }

                        if (place.TimeOC6 != null)
                        {
                            Place_data_multitime new_multitime = new Place_data_multitime();
                            new_multitime.day_week   = E_day_week.SATURDAY;
                            new_multitime.start_date = TimeSpan.Parse($"{place.TimeOC6[0]}:00");
                            new_multitime.end_date   = TimeSpan.Parse($"{place.TimeOC6[1]}:00");
                            new_place.Place_data_multitime.Add(new_multitime);
                        }

                        if (place.TimeOC7 != null)
                        {
                            Place_data_multitime new_multitime = new Place_data_multitime();
                            new_multitime.day_week   = E_day_week.SUNDAY;
                            new_multitime.start_date = TimeSpan.Parse($"{place.TimeOC7[0]}:00");
                            new_multitime.end_date   = TimeSpan.Parse($"{place.TimeOC7[1]}:00");
                            new_place.Place_data_multitime.Add(new_multitime);
                        }

                        new_place.Place_address         = new Place_address();
                        new_place.Place_address.country = place.Country;
                        new_place.Place_address.city    = place.City;
                        new_place.Place_address.street  = place.Street;
                        new_place.Place_address.number  = place.Number;

                        _context.Place.Add(new_place);

                        _context.SaveChanges();
                        _contextTransaction.Commit();
                    }
                    catch
                    {
                        _contextTransaction.Rollback();
                        return(BadRequest("Can't create new place"));
                    }
                }
            }

            return(Ok());
        }
Example #2
0
        public IActionResult EditDatePlace([FromBody] EditDatePlaceModel credentials)
        {
            if (credentials == null)
            {
                return(BadRequest("Send credentials"));
            }

            var  identity = HttpContext.User.Identity as ClaimsIdentity;
            int  id_user  = AccountController.GetUserId(identity);
            bool update   = false;

            if (id_user < 0)
            {
                return(Unauthorized("No user"));
            }

            if (credentials.timeOC1 == null && credentials.timeOC2 == null && credentials.timeOC3 == null &&
                credentials.timeOC4 == null && credentials.timeOC5 == null && credentials.timeOC6 == null &&
                credentials.timeOC7 == null)
            {
                return(BadRequest("No time set"));
            }

            TimeSpan timeOpen;
            TimeSpan timeClose;

            if (credentials.timeOC1 != null)
            {
                timeOpen  = TimeSpan.Parse(credentials.timeOC1[0]);
                timeClose = TimeSpan.Parse(credentials.timeOC1[1]);
                if (timeOpen >= timeClose)
                {
                    return(Unauthorized("Wrong time"));
                }
            }

            if (credentials.timeOC2 != null)
            {
                timeOpen  = TimeSpan.Parse(credentials.timeOC2[0]);
                timeClose = TimeSpan.Parse(credentials.timeOC2[1]);
                if (timeOpen >= timeClose)
                {
                    return(Unauthorized("Wrong time"));
                }
            }

            if (credentials.timeOC3 != null)
            {
                timeOpen  = TimeSpan.Parse(credentials.timeOC3[0]);
                timeClose = TimeSpan.Parse(credentials.timeOC3[1]);
                if (timeOpen >= timeClose)
                {
                    return(Unauthorized("Wrong time"));
                }
            }

            if (credentials.timeOC4 != null)
            {
                timeOpen  = TimeSpan.Parse(credentials.timeOC4[0]);
                timeClose = TimeSpan.Parse(credentials.timeOC4[1]);
                if (timeOpen >= timeClose)
                {
                    return(Unauthorized("Wrong time"));
                }
            }

            if (credentials.timeOC5 != null)
            {
                timeOpen  = TimeSpan.Parse(credentials.timeOC5[0]);
                timeClose = TimeSpan.Parse(credentials.timeOC5[1]);
                if (timeOpen >= timeClose)
                {
                    return(Unauthorized("Wrong time"));
                }
            }

            if (credentials.timeOC6 != null)
            {
                timeOpen  = TimeSpan.Parse(credentials.timeOC6[0]);
                timeClose = TimeSpan.Parse(credentials.timeOC6[1]);
                if (timeOpen >= timeClose)
                {
                    return(Unauthorized("Wrong time"));
                }
            }

            if (credentials.timeOC7 != null)
            {
                timeOpen  = TimeSpan.Parse(credentials.timeOC7[0]);
                timeClose = TimeSpan.Parse(credentials.timeOC7[1]);
                if (timeOpen >= timeClose)
                {
                    return(Unauthorized("Wrong time"));
                }
            }

            using (var _context = new AppDBContext())
            {
                try
                {
                    var record = _context.Place.Where(place => (place.id_place == credentials.place_id))
                                 .FirstOrDefault();
                    if (record == null)
                    {
                        return(BadRequest("Place not exists"));
                    }
                    if (record.id_user != id_user)
                    {
                        return(Unauthorized("Account is not associated with place"));
                    }

                    List <Place_data_multitime> new_times_list = new List <Place_data_multitime>();
                    if (credentials.timeOC1 != null)
                    {
                        Place_data_multitime new_multitime = new Place_data_multitime();
                        new_multitime.day_week   = E_day_week.MONDAY;
                        new_multitime.start_date = TimeSpan.Parse($"{credentials.timeOC1[0]}:00");
                        new_multitime.end_date   = TimeSpan.Parse($"{credentials.timeOC1[1]}:00");
                        new_times_list.Add(new_multitime);
                    }

                    if (credentials.timeOC2 != null)
                    {
                        Place_data_multitime new_multitime = new Place_data_multitime();
                        new_multitime.day_week   = E_day_week.TUESDAY;
                        new_multitime.start_date = TimeSpan.Parse($"{credentials.timeOC2[0]}:00");
                        new_multitime.end_date   = TimeSpan.Parse($"{credentials.timeOC2[1]}:00");
                        new_times_list.Add(new_multitime);
                    }

                    if (credentials.timeOC3 != null)
                    {
                        Place_data_multitime new_multitime = new Place_data_multitime();
                        new_multitime.day_week   = E_day_week.WEDNESDAY;
                        new_multitime.start_date = TimeSpan.Parse($"{credentials.timeOC3[0]}:00");
                        new_multitime.end_date   = TimeSpan.Parse($"{credentials.timeOC3[1]}:00");
                        new_times_list.Add(new_multitime);
                    }

                    if (credentials.timeOC4 != null)
                    {
                        Place_data_multitime new_multitime = new Place_data_multitime();
                        new_multitime.day_week   = E_day_week.THURSDAY;
                        new_multitime.start_date = TimeSpan.Parse($"{credentials.timeOC4[0]}:00");
                        new_multitime.end_date   = TimeSpan.Parse($"{credentials.timeOC4[1]}:00");
                        new_times_list.Add(new_multitime);
                    }

                    if (credentials.timeOC5 != null)
                    {
                        Place_data_multitime new_multitime = new Place_data_multitime();
                        new_multitime.day_week   = E_day_week.FRIDAY;
                        new_multitime.start_date = TimeSpan.Parse($"{credentials.timeOC5[0]}:00");
                        new_multitime.end_date   = TimeSpan.Parse($"{credentials.timeOC5[1]}:00");
                        new_times_list.Add(new_multitime);
                    }

                    if (credentials.timeOC6 != null)
                    {
                        Place_data_multitime new_multitime = new Place_data_multitime();
                        new_multitime.day_week   = E_day_week.SATURDAY;
                        new_multitime.start_date = TimeSpan.Parse($"{credentials.timeOC6[0]}:00");
                        new_multitime.end_date   = TimeSpan.Parse($"{credentials.timeOC6[1]}:00");
                        new_times_list.Add(new_multitime);
                    }

                    if (credentials.timeOC7 != null)
                    {
                        Place_data_multitime new_multitime = new Place_data_multitime();
                        new_multitime.day_week   = E_day_week.SUNDAY;
                        new_multitime.start_date = TimeSpan.Parse($"{credentials.timeOC7[0]}:00");
                        new_multitime.end_date   = TimeSpan.Parse($"{credentials.timeOC7[1]}:00");
                        new_times_list.Add(new_multitime);
                    }

                    var old_times_list = _context.Place_data_multitime
                                         .Where(place => (place.id_place == credentials.place_id)).ToArray();

                    if (old_times_list != null)
                    {
                        int count_old = old_times_list.Length;
                        int count_new = new_times_list.Count;
                        int index     = 0;

                        if (count_old < count_new)
                        {
                            while (count_new > count_old)
                            {
                                var new_record = new Place_data_multitime();
                                new_record.day_week   = new_times_list[count_new - 1].day_week;
                                new_record.id_place   = credentials.place_id;
                                new_record.start_date = new_times_list[count_new - 1].start_date;
                                new_record.end_date   = new_times_list[count_new - 1].end_date;
                                _context.Place_data_multitime.Add(new_record);
                                count_new--;
                            }
                        }
                        else if (count_old > count_new)
                        {
                            while (count_old > count_new)
                            {
                                _context.Place_data_multitime.Remove(old_times_list[count_old - 1]);
                                count_old--;
                            }
                        }

                        while (index < count_new)
                        {
                            old_times_list[index].day_week   = new_times_list[index].day_week;
                            old_times_list[index].start_date = new_times_list[index].start_date;
                            old_times_list[index].end_date   = new_times_list[index].end_date;

                            _context.Place_data_multitime.Update(old_times_list[index]);
                            index++;
                        }

                        update = true;
                        _context.SaveChanges();
                    }
                }
                catch
                {
                    return(BadRequest("Invalid request"));
                }
            }

            if (update)
            {
                return(Ok());
            }
            return(BadRequest("No action"));
        }