<commit_before>﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Description;
using WebServiceSmartKitchen.Models;
using WebServiceSmartKitchen.ViewModels;

namespace WebServiceSmartKitchen.Controllers
{
    public class KitchensController : ApiController
    {
        private ModelContainer db = new ModelContainer();

        // POST: api/Kitchens/Login
        [ActionName("Login")]
        [ResponseType(typeof(Kitchen))]
        public async Task<IHttpActionResult> PostLogin(KitchenLogin login)
        {
            try
            {
                // Members member = await db.MembersSet.FindAsync(login.Email); //Only keys can send, use other method function.
                IEnumerable<Members> memberSearch = from search in db.MembersSet
                                                    where search.Email == login.Email
                                                    select search;
                Members member = new Members();
                member = memberSearch.FirstOrDefault();
                if (member == null)
                {
                    return NotFound();
                }
                else if (member.Password == <PASSWORD>)
                {
                    Kitchen kitchen = await db.KitchenSet.FindAsync(member.Kitchen.Id);
                    return Ok(kitchen);
                }
                else
                {
                    return NotFound();
                }
            }
            catch (Exception e)
            {
                return ResponseMessage(Request.CreateResponse(HttpStatusCode.NotFound, e.Message));
            }

        }


        // GET: api/Kitchens
        public IQueryable<Kitchen> GetKitchenSet()
        {
            return db.KitchenSet;
        }

        // GET: api/Kitchens/5
        [ResponseType(typeof(Kitchen))]
        public async Task<IHttpActionResult> GetKitchen(int id)
        {
            Kitchen kitchen = await db.KitchenSet.FindAsync(id);
            if (kitchen == null)
            {
                return NotFound();
            }

            return Ok(kitchen);
        }

        // PUT: api/Kitchens/5
        [ResponseType(typeof(void))]
        public async Task<IHttpActionResult> PutKitchen(int id, Kitchen kitchen)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != kitchen.Id)
            {
                return BadRequest();
            }

            db.Entry(kitchen).State = System.Data.Entity.EntityState.Modified;

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!KitchenExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }

        // POST: api/Kitchens/Register
        [ActionName("Register")]
        [ResponseType(typeof(Kitchen))]
        public async Task<IHttpActionResult> PostKitchen(KitchenRegister register)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            Kitchen kitchen = new Kitchen();
            Members addMember = new Members();
            kitchen.Name = register.KitchenName;
            addMember.Active = register.Active;
            addMember.Admin = register.Admin;
            addMember.DateOfBirth = register.DateOfBirth;
            addMember.Email = register.Email;
            addMember.Firstname = register.Firstname;
            addMember.Lastname = register.Lastname;
            addMember.Password = <PASSWORD>;
            kitchen.Members.Add(addMember);
            db.KitchenSet.Add(kitchen);
            await db.SaveChangesAsync();

            //return CreatedAtRoute("DefaultApi", new { id = kitchen.Id }, kitchen);
            return Ok(kitchen);
        }

        // DELETE: api/Kitchens/5
        [ResponseType(typeof(Kitchen))]
        public async Task<IHttpActionResult> DeleteKitchen(int id)
        {
            Kitchen kitchen = await db.KitchenSet.FindAsync(id);
            if (kitchen == null)
            {
                return NotFound();
            }

            db.KitchenSet.Remove(kitchen);<commit_msg>check if email is unique<commit_after>﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Description;
using WebServiceSmartKitchen.Models;
using WebServiceSmartKitchen.ViewModels;

namespace WebServiceSmartKitchen.Controllers
{
    public class KitchensController : ApiController
    {
        private ModelContainer db = new ModelContainer();

        // POST: api/Kitchens/Login
        [ActionName("Login")]
        [ResponseType(typeof(Kitchen))]
        public async Task<IHttpActionResult> PostLogin(KitchenLogin login)
        {
            try
            {
                // Members member = await db.MembersSet.FindAsync(login.Email); //Only keys can send, use other method function.
                IEnumerable<Members> memberSearch = from search in db.MembersSet
                                                    where search.Email == login.Email
                                                    select search;
                Members member = new Members();
                member = memberSearch.FirstOrDefault();
                if (member == null)
                {
                    return NotFound();
                }
                else if (member.Password == <PASSWORD>)
                {
                    Kitchen kitchen = await db.KitchenSet.FindAsync(member.Kitchen.Id);
                    return Ok(kitchen);
                }
                else
                {
                    return NotFound();
                }
            }
            catch (Exception e)
            {
                return ResponseMessage(Request.CreateResponse(HttpStatusCode.NotFound, e.Message));
            }

        }


        // GET: api/Kitchens
        public IQueryable<Kitchen> GetKitchenSet()
        {
            return db.KitchenSet;
        }

        // GET: api/Kitchens/5
        [ResponseType(typeof(Kitchen))]
        public async Task<IHttpActionResult> GetKitchen(int id)
        {
            Kitchen kitchen = await db.KitchenSet.FindAsync(id);
            if (kitchen == null)
            {
                return NotFound();
            }

            return Ok(kitchen);
        }

        // PUT: api/Kitchens/5
        [ResponseType(typeof(void))]
        public async Task<IHttpActionResult> PutKitchen(int id, Kitchen kitchen)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != kitchen.Id)
            {
                return BadRequest();
            }

            db.Entry(kitchen).State = System.Data.Entity.EntityState.Modified;

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!KitchenExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }

        // POST: api/Kitchens/Register
        [ActionName("Register")]
        [ResponseType(typeof(Kitchen))]
        public async Task<IHttpActionResult> PostKitchen(KitchenRegister register)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            IEnumerable<Members> memberSearchExist = from search in db.MembersSet
                                                where search.Email == register.Email
                                                select search;
            if (memberSearchExist.Any())
            {
                return Conflict();
            }
            Kitchen kitchen = new Kitchen();
            Members addMember = new Members();
            kitchen.Name = register.KitchenName;
            addMember.Active = register.Active;
            addMember.Admin = register.Admin;
            addMember.DateOfBirth = register.DateOfBirth;
            addMember.Email = register.Email;
            addMember.Firstname = register.Firstname;
            addMember.Lastname = register.Lastname;
            addMember.Password = <PASSWORD>;
            kitchen.Members.Add(addMember);
            db.KitchenSet.Add(kitchen);
            await db.SaveChangesAsync();

            //return CreatedAtRoute("DefaultApi", new { id = kitchen.Id }, kitchen);
            return Ok(kitchen);
        }

        // DELETE: api/Kitchens/5
        [ResponseType(typeof(Kitchen))]
        public async Task<IHttpActionResult> DeleteKitchen(int id)
        {
            Kitchen kitchen = await db.KitchenSet.FindAsync(id);
            if (kitchen == null)
            {
                return NotFound();
            }

            db.KitchenSet.Remove(kitchen);
{'fraction_non_alphanumeric': 0.086924914321321, 'fraction_numerical': 0.0006231176653858137, 'mean_word_length': 1.4038941587618572, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3311479', 'n_tokens_mistral': 2373, 'n_tokens_neox': 2294, 'n_words': 472}